package com.projectxxxxxxxxxx.screens;

import java.util.TimerTask;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;
import android.widget.RemoteViews;

import com.projectxxxxxxxxxx.Constants;
import com.projectxxxxxxxxxx.R;
import com.projectxxxxxxxxxx.info.Info;

public class HardwareScreen extends Screen {

    private final long WIDGET_UPDATE_INTERVAL = 500;


    String cpuUsage = "";
    String cpuCore = "";
    
    String ramUsage = "";
    String ramTotal = "";
    
    String internalUsage = "";
    String internalTotal = "";
    
    String externalUsage = "";
    String externalTotal = "";
    
    
    public HardwareScreen(final Context context, final int id, String widgetClassName) {
        super(context, id, widgetClassName);

        // fill actionsView with views you need
    }

    @Override
    public void updateAllView(final RemoteViews views) {

        pingTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                requestRedraw(Constants.ACTION_UPDATE_HARDWARE_SCREEN);
            }
        }, 0);
    }


    @Override
    public void updateView(final Intent intent, final String action, final RemoteViews views) {
        // use enums here

        LoadEnums loadEnums = new LoadEnums(context, views);
        loadEnums.execute(action);

        pingTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                requestRedraw(Constants.ACTION_UPDATE_HARDWARE_SCREEN);
            }
        }, WIDGET_UPDATE_INTERVAL);
    }
    
    static int ind;
    
    public class LoadEnums extends AsyncTask<String, Void, String> {

    	
    	
        final Context context;
        RemoteViews views;
        String[] args; 

        public LoadEnums(final Context context, final RemoteViews views) {
            this.views = views;
            this.context = context;
        }

        String cpuUsage = "";
        String cpuCore = "";        
        String ramUsage = "";
        String ramTotal = "";        
        String internalUsage = "";
        String internalTotal = "";        
        String externalUsage = "";
        String externalTotal = "";
        String battery = "";
        


        @Override
        protected String doInBackground(final String... args) {
            this.args = args;
            
            cpuUsage = Info.CPU_USAGE.get().get(Info.INFO_CPU_USAGE);
            cpuCore = Info.CPU_DATA.get().get(Info.INFO_CPU_CORES);
            
            ramUsage = Info.MEMORY_USAGE.get().get(Info.INFO_MEMORY_USAGE);
            ramTotal = Info.MEMORY_DATA.get().get(Info.INFO_MEMORY_TOTAL);
            
            internalUsage = Info.STORAGE_INTERNAL_USAGE.get().get(Info.INFO_STORAGE_INTERNAL_USAGE);
            internalTotal = Info.STORAGE_INTERNAL_DATA.get().get(Info.INFO_STORAGE_INTERNAL_TOTAL);
            
            externalUsage = Info.STORAGE_EXTERNAL_USAGE.get().get(Info.INFO_STORAGE_EXTERNAL_USAGE);
            externalTotal = Info.STORAGE_EXTERNAL_DATA.get().get(Info.INFO_STORAGE_EXTERNAL_USED);
            
            battery = Info.BATTERY.get().get(Info.INFO_BATERRY_LAVEL);
            
            return null;
        }

        @Override
        protected void onPostExecute(final String result) {

            IndicatorCellSet ind = new IndicatorCellSet(views, Integer.parseInt(battery), IndicatorParams.BATTERY);
            views = ind.fillViewWithCell();

			ind = new IndicatorCellSet(views, Integer.parseInt(internalUsage), IndicatorParams.INTERNAL);
            views = ind.fillViewWithCell();

            views.setViewVisibility(R.id.robo_sd_usage, Environment.getExternalStorageState().equals(Environment.MEDIA_UNMOUNTED) ? View.GONE : View.VISIBLE);
			ind = new IndicatorCellSet(views, Integer.parseInt(externalUsage), IndicatorParams.SDCARD);
            views = ind.fillViewWithCell();
            
            ind = new IndicatorCellSet(views, Integer.parseInt(cpuUsage), IndicatorParams.CPU);
            views = ind.fillViewWithCell();
            
            ind = new IndicatorCellSet(views, Integer.parseInt(ramUsage), IndicatorParams.RAM);
			views = ind.fillViewWithCell();

			RemoteViews view = views;
			view.setTextViewText(R.id.robo_battery_label, battery);
			views = view;
			 
            updateWidget(views);
        }

    }

    public class IndicatorCellSet {
        private final RemoteViews views;
        private final int currentCell;
        private final IndicatorParams type;

        public IndicatorCellSet(final RemoteViews views, final int currentCell, final IndicatorParams type) {
            this.views = views;
            this.currentCell = currentCell;
            this.type = type;
        }

        public RemoteViews fillViewWithCell() {

            int currentCellImageInd = (currentCell * type.getMaxCells() / 100);
            if (currentCellImageInd == type.getMaxCells()) {
                currentCellImageInd = type.getMaxCells() - 1;
            }

            views.setImageViewResource(type.getViewId(), type.getCellsImageMas()[currentCellImageInd]);

            return views;
        }

        public RemoteViews getViews() {
            return views;
        }

        public int getCurrentCell() {
            return currentCell;
        }

        public IndicatorParams getType() {
            return type;
        }
    };

    public enum IndicatorParams {
		BATTERY {

            int[] mas = {
                    R.drawable.battery_1,
                    R.drawable.battery_2,
                    R.drawable.battery_3,
                    R.drawable.battery_4,
                    R.drawable.battery_5,
                    R.drawable.battery_6,
                    R.drawable.battery_7,
                    R.drawable.battery_8,
                    R.drawable.battery_9,
                    R.drawable.battery_10,
                    R.drawable.battery_11,
                    R.drawable.battery_12,
                    R.drawable.battery_13,
                    R.drawable.battery_14,
                    R.drawable.battery_15,
                    R.drawable.battery_16,
                    R.drawable.battery_17,
                    R.drawable.battery_18,
                    R.drawable.battery_19,
                    R.drawable.battery_20,
                    R.drawable.battery_21,
                    R.drawable.battery_22,
                    R.drawable.battery_23,
                    R.drawable.battery_24,
            };

            @Override
            public int getViewId() {
                return R.id.robo_battery_usage;
            }

            // battery 24 cells
            @Override
            public int getMaxCells() {
                return mas.length;
            }

            @Override
            public int[] getCellsImageMas() {

                return mas;
            }

        },

        SDCARD {

            int[] mas = {
                    R.drawable.sd_9,
                    R.drawable.sd_8,
                    R.drawable.sd_7,
                    R.drawable.sd_6,
                    R.drawable.sd_5,
                    R.drawable.sd_4,
                    R.drawable.sd_3,
                    R.drawable.sd_2,
                    R.drawable.sd_1
            };

            @Override
            public int getViewId() {
                return R.id.robo_sd_usage;
            }

            // sdcard 9 cells
            @Override
            public int getMaxCells() {
                return mas.length;
            }

            @Override
            public int[] getCellsImageMas() {
                return mas;
            }

        },

        INTERNAL {

            int[] mas = {
                    R.drawable.internal_9,
                    R.drawable.internal_8,
                    R.drawable.internal_7,
                    R.drawable.internal_6,
                    R.drawable.internal_5,
                    R.drawable.internal_4,
                    R.drawable.internal_3,
                    R.drawable.internal_2,
                    R.drawable.internal_1
            };

            @Override
            public int getViewId() {
                return R.id.robo_internal_usage;
            }

            // internal 9 cells
            @Override
            public int getMaxCells() {
                return mas.length;
            }

            @Override
            public int[] getCellsImageMas() {
                return mas;
            }

        },

        RAM {

            int[] mas = {
            		R.drawable.ram_1,
            		R.drawable.ram_2,
            		R.drawable.ram_3,
            		R.drawable.ram_4,
            		R.drawable.ram_5,
            		R.drawable.ram_6,
            		R.drawable.ram_7,
            		R.drawable.ram_8,
            		R.drawable.ram_9,
            		R.drawable.ram_10,
            		R.drawable.ram_11,
            		R.drawable.ram_12,
            		R.drawable.ram_13,
            		R.drawable.ram_14,
            		R.drawable.ram_15,
            		R.drawable.ram_16,
            		R.drawable.ram_17,
            		R.drawable.ram_18,
            		R.drawable.ram_19,
            		R.drawable.ram_20,
            		R.drawable.ram_21,
            		R.drawable.ram_22,
            		R.drawable.ram_23,
            		R.drawable.ram_24,
            		R.drawable.ram_25,
            		R.drawable.ram_26,
            		R.drawable.ram_27,
            		R.drawable.ram_28,
            		R.drawable.ram_29,
            		R.drawable.ram_30,
            		R.drawable.ram_31,
            };

            @Override
            public int getViewId() {
                return R.id.robo_ram_usage;
            }

            // ram 24 cells
            @Override
            public int getMaxCells() {
                return mas.length;
            }

            @Override
            public int[] getCellsImageMas() {

                return mas;
            }

        },

        CPU {
            // change to image ids
            int[] mas = {
            		R.drawable.cpu_1,
            		R.drawable.cpu_2,
            		R.drawable.cpu_3,
            		R.drawable.cpu_4,
            		R.drawable.cpu_5,
            		R.drawable.cpu_6,
            		R.drawable.cpu_7,
            		R.drawable.cpu_8,
            		R.drawable.cpu_9,
            		R.drawable.cpu_10,
            		R.drawable.cpu_11,
            		R.drawable.cpu_12,
            		R.drawable.cpu_13,
            		R.drawable.cpu_14,
            		R.drawable.cpu_15,
            		R.drawable.cpu_16,
            		R.drawable.cpu_17,
            		R.drawable.cpu_18,
            		R.drawable.cpu_19,
            		R.drawable.cpu_20,
            		R.drawable.cpu_21,
            		R.drawable.cpu_22,
            		R.drawable.cpu_23,
            		R.drawable.cpu_24,
            		R.drawable.cpu_25,
            		R.drawable.cpu_26,
            		R.drawable.cpu_27,
            		R.drawable.cpu_28,
            		R.drawable.cpu_29,
            		R.drawable.cpu_30,
            		R.drawable.cpu_31,
            };

            @Override
            public int getViewId() {
                return R.id.robo_cpu_usage;
            }

            // cpu 10 cells
            @Override
            public int getMaxCells() {
                return mas.length;
            }

            @Override
            public int[] getCellsImageMas() {
                return mas;
            }

        };

        public abstract int getViewId();
        public abstract int getMaxCells();
        public abstract int[] getCellsImageMas();
    };

    public enum LabelParams {
        A_1_6 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_1_6;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_1_6;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_1_6_rotate;
            }
        },
        A_2_0 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_2_0;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_2_0;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_2_0_rotate;
            }
        },
        A_2_0_1 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_2_0_1;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_2_0_1;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_2_0_1_rotate;
            }
        },
        A_2_1 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_2_1;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_2_1;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_2_1_rotate;
            }
        },
        A_2_1_u {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_2_1_update1;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_2_1;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_2_1_rotate;
            }
        },
        A_2_2 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_2_2;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_2_2;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_2_2_rotate;
            }
        },
        A_2_3 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_2_3;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_2_3;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_2_3_rotate;
            }
        },
        A_2_3_3 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_2_3_3;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_2_3_3;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_2_3_3_rotate;
            }
        },
        A_2_3_4 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_2_3_4;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_2_3_4;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_2_3_4_rotate;
            }
        },
        A_2_3_5 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_2_3_5;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_2_3_5;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_2_3_5_rotate;
            }
        },
        A_2_3_6 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_2_3_6;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_2_3_6;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_2_3_6_rotate;
            }
        },
        A_2_3_7 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_2_3_7;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_2_3_7;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_2_3_7_rotate;
            }
        },
        A_3_0 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_3_0;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_3_0;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_3_0_rotate;
            }
        },
        A_3_1 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_3_1;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_3_1;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_3_1_rotate;
            }
        },
        A_3_2 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_3_2;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_3_2;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_3_2_rotate;
            }
        },
        A_3_2_1 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_3_2_1;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_3_2_1;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_3_2_1_rotate;
            }
        },
        A_3_2_2 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_3_2_2;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_3_2_2;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_3_2_2_rotate;
            }
        },
        A_3_2_4 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_3_2_4;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_3_2_4;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_3_2_4_rotate;
            }
        },
        A_3_2_6 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_3_2_6;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_3_2_6;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_3_2_6_rotate;
            }
        },
        A_4_0 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_4_0;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_4_0;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_4_0_rotate;
            }
        },
        A_4_0_1 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_4_0_1;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_4_0_1;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_4_0_1_rotate;
            }
        },
        A_4_0_2 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_4_0_2;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_4_0_2;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_4_0_2_rotate;
            }
        },
        A_4_0_3 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_4_0_3;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_4_0_3;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_4_0_3_rotate;
            }
        },
        A_4_0_4 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_4_0_4;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_4_0_4;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_4_0_4_rotate;
            }
        },
        A_4_1 {
            @Override
            public String getName() {
                return Constants.LABEL_NAME_4_1;
            }

            @Override
            public int getimageResId() {
                return R.drawable.android_4_1;
            }

            @Override
            public int getimageResIdRotate() {
                return R.drawable.android_4_1_rotate;
            }
        };

        public abstract String getName();
        public abstract int getimageResId();

        public abstract int getimageResIdRotate();
    };
}
